package com.system.ad.utlis;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;
@Slf4j
public class CommonUtils {

    public static <K, V> V getterCreate(K key, Map<K, V> map, Supplier<V> factory) {
        return map.computeIfAbsent(key, k -> factory.get());
    }

    public static String stringConcat(String... args) {

        StringBuilder result = new StringBuilder();
        for (String arg : args) {
            result.append(arg);
            result.append("-");
        }
        result.deleteCharAt(result.length() - 1);
        return result.toString();
    }

    //日期格式转换 cst日期会加上8小时
    public static Date parseStringDate(String dateString){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        try {
            return DateUtils.addHours(simpleDateFormat.parse(dateString),-8);
        } catch (ParseException e) {
            log.error("parseStringDate error :{}",dateString);
            return null;
        }
    }
}
